<?php

// 连接数据库
$dsn = 'mysql:dbname=test1;host=127.0.0.1';
$user = 'root';
$password = 'root';
try {
    $pdo = new PDO($dsn, $user, $password);
    echo "连接成功";

    // sql语句
    $sql="select * from tb_user;";
    // 查到pdo对象结果集
    $res=$pdo->query($sql);
    // 一行一行的循环
    echo "<table border='1' width='800'>";
    foreach ($res as $row){
        echo "<tr>";
        echo "<td>".$row["id"]."</td>";
        echo "<td>".$row["name"]."</td>";
        echo "<td>".$row["text"]."</td>";
        echo "<td>".$row["age"]."</td>";
        echo "<td>".$row["sex"]."</td>";
        echo  "</tr>";
        //var_dump($row);
    }
    echo  "</table>";

} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
